﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication01
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetDataTable(sql);
            gv1.DataSource = dt;
            gv1.DataBind();
        }
        private DataTable Data()
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetDataTable(sql);
            return dt;
        }

        //新增
        protected void Unnamed_Click(object sender, EventArgs e)
        {
            var dt = Data().Copy();
            dt.Rows.Add(dt.NewRow());

            gv1.EditIndex = dt.Rows.Count - 1;
            gv1.DataSource = dt;
            gv1.DataBind();
        }
        //取消编辑
        protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv1.EditIndex = -1;
            FillData();
        }
        //编辑
        protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv1.EditIndex = e.NewEditIndex;
            FillData();
        }
        //删除
        protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GeVales(e.RowIndex,0);
            var sql = string.Format("delete from Articles where id={0}",id);
            DbHelper.GetDataTable(sql);
            gv1.EditIndex = -1;
            FillData();
        }
        //更改
        protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GeVales(e.RowIndex, 0);
            var isOK = int.TryParse(idString,out int id);
            var title = GeVales(e.RowIndex,1);
            var content = GeVales(e.RowIndex,2);
            var author = GeVales(e.RowIndex,3);
            var remakrs = GeVales(e.RowIndex,4);

            if (isOK)
            {
                //更改
                var sql1 = string.Format("update Articles set Title='{0}',Content='{1}', Author='{2}',Remarks='{3}' where id='{4}'",title,content,author,remakrs,id);
                DbHelper.Add(sql1);
            }
            else
            {
                //添加
                var sql2 = string.Format("insert into Articles (Title,Content,Author,Remarks)values('{0}','{1}','{2}','{3}')",title,content,author,remakrs,id);
                DbHelper.Add(sql2);
            }

            gv1.EditIndex = -1;
            FillData();
        }

        private string GeVales(int rowIndex, int v)
        {
            var c = gv1.Rows[rowIndex].Cells[v];
            var value = c.Controls.Count > 0 ? ((TextBox)c.Controls[0]).Text : c.Text;
            return value;
        }
    }
}

